import loginEjs from '$v/login'
import adminApi from '$a/adminApi'

async function loginBtn(){
    let {adminName, password} = document.loginForm
    adminName = adminName.value.trim()
    password = password.value.trim()    
    if(!adminName.length){
        toastr.error('请输入用户名')
        return
    }else if(!password.length){
        toastr.error('请输入密码')
        return
    }

    let {code,msg,data} = await adminApi.login({adminName,password})
    if(code){
        console.log(1);
        toastr.error(msg)
    }else{
        console.log(2);
        let {token} = data
        localStorage.setItem('token',token)
        ROUTER.go(ADMINLIST)
    }
}

export default function loginController(){
    return async (req,res)=>{
        res.render(loginEjs())
        $('#loginBtn').on('click',loginBtn)
    }
}